/* 
 * ajax验证用户名是否已经被注册
 */
var xmlHttp;
var strMsg;

function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  } 
var url="ajax/checkUserName.php";
url=url+"?userName="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged ;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
     if(xmlHttp.responseText == "0"){
         strMsg = "该用户名未被注册，可以使用。";
     }
     if(xmlHttp.responseText == "1"){
         strMsg = "该用户名已被注册，请重新输入！";
     }
     document.getElementById("txtHint").innerHTML=strMsg;
     document.getElementById("validFlag").value=xmlHttp.responseText;
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

/**
 * 检查输入框是否为空
 */
function checkEmpty(str){
    if(str.length == 0 || str == "" || str == null){
        document.getElementById("txtHint").innerHTML="用户名不能为空！";
        return false;
    }
    return true;
}

/**
 * 表单提交
 */
function doSubmit(formID){
    var userName=document.getElementById("userName").value;
    var validFlag=document.getElementById("validFlag").value;
    showHint(userName);
    if(checkEmpty(userName) && validFlag =="0"){
      formID.submit(); 
    }
    return false;    
}